package com.op.teachingliteapp.service;

import java.util.HashMap;
import java.util.Map;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;

import com.android.volley.Response;
import com.op.teachingliteapp.common.Common;
import com.op.teachingliteapp.common.api.teachinglite.CourseUrl;
import com.op.teachingliteapp.database.dao.CourseDao;
import com.op.teachingliteapp.util.http.VolleySingleton;

public class CourseService extends BaseService {

	@Override
	protected void parse(String response) {
		try {
			JSONObject jsonObject = new JSONObject(response);
			if (!jsonObject.isNull(Common.JsonResult.RESULT_MAP)) {
				content = jsonObject.getJSONObject(Common.JsonResult.RESULT_MAP).getJSONObject(
						Common.JsonResult.LOGIN_USER);
			}
			message = jsonObject.getString(Common.JsonResult.MESSAGE);
			status = jsonObject.getString(Common.JsonResult.STATUS);
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 联网获取当前用户课程
	 */
	public static void syncCourses(Response.Listener<String> listener,
			Response.ErrorListener errorListener) {
		Map<String, String> params = new HashMap<String, String>();
		params.put(Common.Post.USER_ID, UserService.getUserId());
		VolleySingleton.getInstance().load(CourseUrl.GET_COURSE_BY_USER_ID, params, listener,
				errorListener);
	}

	/**
	 * 清空课程本地数据库
	 */
	public static void clear(Context context) {
		// 清空所有课程
		CourseDao courseDao = new CourseDao(context, UserService.getUserId());
		courseDao.empty();
	}

}
